/*
 * Copyright (c) GoKeep by gokeep.org. 2022-2023. All rights reserved
 */
package org.gokeep.common.util;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.gokeep.common.constant.enums.ClientTypeEnum;
import org.gokeep.common.dto.UserContextInfo;

@Slf4j
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class UserContext {
    private static final ThreadLocal<UserContextInfo> CONTEXT = new ThreadLocal<>();

    /**
     * 传入用户上下文信息
     *
     * @param context
     */
    public static void put(UserContextInfo context) {
        CONTEXT.set(context);
    }

    /**
     * 获取用户上下文信息
     *
     * @return
     */
    public static UserContextInfo getUserContextInfo() {
        return CONTEXT.get();
    }

    /**
     * 获取当前用户ID
     *
     * @return
     */
    public static String getUserId() {
        return getUserContextInfo().getUserId();
    }

    /**
     * 获取当前用户名
     *
     * @return
     */
    public static String getUsername() {
        return getUserContextInfo().getUsername();
    }

    /**
     * 获取客户端类型
     *
     * @return
     */
    public static ClientTypeEnum getClientType() {
        return getUserContextInfo().getClientType();
    }

    /**
     * 获取当前Token
     *
     * @return
     */
    public static String token() {
        return getUserContextInfo().getToken();
    }
}
